//
//  BSWelcomeViewController.swift
//  mySwiftWeibo
//
//  Created by apple on 16/7/21.
//  Copyright © 2016年 itcast沈庆伟. All rights reserved.
//



import UIKit
import SnapKit
import SDWebImage



class BSWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        
       //设置界面
        setupUI()
        
        //获取用户信息之前显示一次用户头像
        self.setIcon()
        
        //获取用户信息
        BSUserAccountViewModel.sharedUserAccountViewModel.loadUserInfo { (error) -> () in
            if error != nil{
                print("获取用户数据失败")
                return
            }
            
            //获取信息成功
            self.setIcon()
        }
    }
    
    ///设置头像
    private func setIcon(){
        if let avatar_large = BSUserAccountViewModel.sharedUserAccountViewModel.userAccount?.avatar_large{
            //设置图片是在加载完用户数据后才去设置,加载用户数据慢,前面不会显示出这个头像
            //使用SDWebImage下载图片
            let url = NSURL(string: avatar_large)
            self.headView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        startAnimation()
    }
    
    ///头像动画
    private func startAnimation(){
        let offY = -(UIScreen.mainScreen().bounds.height-160)
        self.headView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(self.view).offset(offY)
        }
        /*
        UIView.animateWithDuration(1) { () -> Void in
        self.view.layoutIfNeeded()
        }
        */
        //弹簧动画效果
        UIView.animateWithDuration(animationInterval, delay: 0.2, usingSpringWithDamping: 0.7, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(animationInterval, animations: { () -> Void in
                    self.welcomLabel.alpha=1
                    }, completion: { (_) -> Void in
                        AppDelegate.switchRootViewController(BSMainTabBarController())
                })
        }
    }

    ///设置界面
    private func setupUI(){
        //添加
        self.view.addSubview(bgView)
        self.view.addSubview(headView)
        self.view.addSubview(welcomLabel)
        
        //约束
        bgView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.view)
        }
        
        headView.snp_makeConstraints{ (make) -> Void in
            make.centerX.equalTo(self.view.snp_centerX)
            make.bottom.equalTo(self.view).offset(-160)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        
        welcomLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view.snp_centerX)
            make.top.equalTo(headView.snp_bottom).offset(16)
        }
    }
    
    
    //MARK: - 懒加载
    private lazy var bgView: UIImageView=UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var headView: UIImageView={
       let imageView=UIImageView(image: UIImage(named: "avatar_default_big"))
        //切圆
        imageView.layer.cornerRadius=42.5
        imageView.layer.masksToBounds=true
        imageView.contentMode = UIViewContentMode.ScaleAspectFill
        
        return imageView
    }()
    
    private lazy var welcomLabel: UILabel={
        let label=UILabel()
        label.text="欢迎归来"
        label.font=UIFont.systemFontOfSize(18)
        label.sizeToFit()
        label.alpha=0
        return label
    }()
        
    


}
